#!/bin/sh

tubeFeeder="python3 mod_tubeFeeder.py"
errorReporter="../common/report/errorReporter.sh"

master_lists="../common/master_lists"

log_date=`date +%y-%m-%d-%H-%M`

# prepare command to append stdout to current log file 
log="tee -a logs/$log_date.log"

# pull the master done list into the list subdirectory
flock $master_lists/$list_name -c "cp $master_lists/doneList lists/masterList" 2>&1 | $log

# get parsed links and upload to IA using TubeUp
$tubeFeeder 2>&1 | $log

# BUG TODO this appears to check `tee` return value, not the tube feeder
# print error if tubeFeeder.py returned non-zero
return_value=$?
if test $return_value -ne 0
then
echo "\033[31mtubeFeeder.py returned $return_value\033[0m" | $log
fi

# remove duplicates from local lists then safely append local lists to master lists
for list in lists/*
do
# isolate filename
list_name=`echo $list | sed "s/.*\///"`

# ignore the local copy of the master done list
if test $list_name = masterList
then
    continue
fi

# sort and remove duplicates from this directory's list
cat $list | sort | uniq > ${list}_temp
mv ${list}_temp $list

# safely append this directory's list to the corresponding master list and remove duplicates
flock $master_lists/$list_name -c "cat $master_lists/$list_name $list | sort | uniq > $master_lists/${list_name}_temp; mv $master_lists/${list_name}_temp $master_lists/$list_name" 2>&1 | $log
done

# safely process log to report errors
flock $errorReporter -c "sh $errorReporter 'logs/$log_date.log'"
